package com.vipheyue.bigcompany.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dd.processbutton.iml.ActionProcessButton;
import com.lidroid.xutils.util.LogUtils;
import com.vipheyue.bigcompany.R;
import com.vipheyue.bigcompany.domain.Company;
import com.vipheyue.bigcompany.domain.MyUser;
import com.vipheyue.bigcompany.utils.ProgressGenerator;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class SetPasswordActivity extends AppCompatActivity implements ProgressGenerator.OnCompleteListener {

    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.btn_register_commit)
    ActionProcessButton btn_register_commit;
    @Bind(R.id.tv_Menu_Title)
    TextView tv_Menu_Title;
    @Bind(R.id.rl_menu_left)
    RelativeLayout rl_menu_left;
    @Bind(R.id.rl_menu_right)
    RelativeLayout rl_menu_right;
    private String commitedPhone;
    private ProgressGenerator progressGenerator;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_password);
        ButterKnife.bind(this);
        commitedPhone = getIntent().getStringExtra("commitedPhone");
        LogUtils.customTagPrefix = "xUtils";
        LogUtils.allowI = true;
//        LogUtils.i(commitedPhone);
//        commitedPhone = "18584500435";
        progressGenerator = new ProgressGenerator(this);

        btn_register_commit.setMode(ActionProcessButton.Mode.ENDLESS);
        type = getIntent().getStringExtra("type");
        tv_Menu_Title.setText(type);
    }


    private void registerUser() {
        Company user = new Company();
        user.setUserType("normalUser");
        user.setUsername(commitedPhone);
        user.setPassword(etPassword.getText().toString().trim());
        //TODO 这里放到JNI里面 需要加密
        user.setUserHiddenPassword(etPassword.getText().toString().trim());
        user.signUp(this, new SaveListener() {
            @Override
            public void onSuccess() {
//                startActivity(new Intent(SetPasswordActivity.this, loginActivity.class));
                finish();
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(SetPasswordActivity.this, "设置密码失败" + msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void upDatePassWord(String realPassword) {

        Company user = new Company();
        user.setUserHiddenPassword(etPassword.getText().toString().trim());
        user.updateCurrentUserPassword(this, realPassword, etPassword.getText().toString().trim(), new UpdateListener() {

            @Override
            public void onSuccess() {
                Toast.makeText(SetPasswordActivity.this, "密码修改成功，可以用新密码进行登录啦", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(SetPasswordActivity.this, loginActivity.class));
                finish();
            }

            @Override
            public void onFailure(int code, String msg) {
                Log.i("smile", "密码修改失败：" + msg + "(" + code + ")");
                Toast.makeText(SetPasswordActivity.this, "密码修改失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getUserRealPassWord(String username) {
        Company userInfo = BmobUser.getCurrentUser(this, Company.class);
        BmobQuery<MyUser> query = new BmobQuery<MyUser>();
        query.addWhereEqualTo("username", username);
        query.findObjects(this, new FindListener<MyUser>() {
            @Override
            public void onSuccess(List<MyUser> object) {
                if (object != null && object.size() > 0) {
                    String hiddenPassWord = object.get(0).getUserHiddenPassword();
                    //TODO 这里需要解密
                    String realPassword = hiddenPassWord;
                    upDatePassWord(realPassword);
                } else {
                    Toast.makeText(SetPasswordActivity.this, "密码修改失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(int code, String msg) {
                LogUtils.i("查询失败：" + msg);
                Toast.makeText(SetPasswordActivity.this, "密码修改失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onComplete() {

    }

    @OnClick({R.id.rl_menu_left, R.id.btn_register_commit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_menu_left:
                finish();
                break;
            case R.id.btn_register_commit:
                if (etPassword.getText().toString().trim().length() < 6) {
                    Toast.makeText(SetPasswordActivity.this, "密码不得小于6位", Toast.LENGTH_SHORT).show();
                } else {
                    progressGenerator.start(btn_register_commit);
                    if (type.contains("注册")) {
                        registerUser();
                    } else {
                        getUserRealPassWord(commitedPhone);
                    }
                }
                break;
        }
    }
}